# -*- coding:utf-8 -*-
import importlib
#from pathlib import Path
from ScadaService.service import ScadaService, HttpService, ModbusService
from ControlService.service import ControlService
from nameko.runners import ServiceRunner
from nameko.rpc import rpc
#from nameko.testing.utils import get_container
import os


#root = Path(__file__).parent


def run_all():
    # services = {}
    # for f in root.iterdir():
    #     if f.is_dir() and f.name.endswith("Service"):
    #         name = f.name
    #         # try:
    #         service = importlib.import_module(name + '.service')
    #         services[name] = service
    #         # except:
    #         #     pass
    AMQP_URI = 'pyamqp://guest:guest@localhost:5672'
    runner = ServiceRunner(config={'AMQP_URI': AMQP_URI})
    # runner = ServiceRunner(config={"AMQP_URI: pyamqp: // ${RABBITMQ_USER: guest}:${RABBITMQ_PASSWORD: guest} @${RABBITMQ_HOST: localhost"})
    # for name, service in services.items():
    #     cls = getattr(service, name)
    #     runner.add_service(cls)

    runner.add_service(ScadaService)
    runner.add_service(ModbusService)
    runner.add_service(HttpService)
    runner.add_service(ControlService)

    #container_a = get_container(runner, ScadaService)

    print("starting")
    runner.start()
    print("stoping")
    #runner.stop()

    # # create a container
    # container = ServiceContainer(ScadaService, config={"AMQP_URI": "amqp://guest:guest@localhost:5672"})
    #
    # # ``container.extensions`` exposes all extensions used by the service
    # service_extensions = list(container.extensions)
    #
    # # start service
    # print("container start!")
    # container.start()
    # print("container stop!")
    # container.stop()


if __name__ == '__main__':
    #run_all()
    n = os.system("nameko run ScadaService.service")>>8
    print(n)